 /**
  *
  *地图工具控件
  */
 MapToolWidget=OpenLayers.Class(BaseWidget,{
 	sketchSymbolizers: null,
 	style : null,
 	styleMap : null,
 	measureControls : null, 
 	initialize : function() {
 		this.sketchSymbolizers={
                "Point": {
                    pointRadius: 4,	
                    graphicName: "square",
                    fillColor: "white",
                    fillOpacity: 1,
                    strokeWidth: 1,
                    strokeOpacity: 1,
                    strokeColor: "#333333"
                },
                "Line": {
                    strokeWidth: 3,
                    strokeOpacity: 1,
                    strokeColor: "#666666",
                    strokeDashstyle: "dash"
                },
                "Polygon": {
                    strokeWidth: 2,
                    strokeOpacity: 1,
                    strokeColor: "red",
                    fillColor: "white",
                    fillOpacity: 0.3
                }
            };
            this.style=new OpenLayers.Style();
            this.style.addRules([
                new OpenLayers.Rule({symbolizer: this.sketchSymbolizers})
            ]);
            
            this.styleMap=new OpenLayers.StyleMap({"default": this.style});
          
            this.measureControls={
                line: new OpenLayers.Control.Measure(
                    OpenLayers.Handler.Path, {
                        persist: true,
                        type :'line',
                        handlerOptions: {
                            layerOptions: {styleMap: this.styleMap}
                        }
                    }
                ),
                polygon: new OpenLayers.Control.Measure(
                    OpenLayers.Handler.Polygon, {
                        persist: true,
                        type :'polygon',
                        handlerOptions: {
                            layerOptions: {styleMap: this.styleMap}
                        }
                    }
                ),
                boxZooIn : new OpenLayers.Control.Measure(
                    OpenLayers.Handler.RegularPolygon, {
                        persist: false,
                        type :'boxZooIn',
                        handlerOptions: {
                        	irregular: true,
                            layerOptions: {styleMap: this.styleMap}
                        }
                    }
                ),
                boxZooOut : new OpenLayers.Control.Measure(
                    OpenLayers.Handler.RegularPolygon, {
                        persist: false,
                        type :'boxZooOut',
                        handlerOptions: {
                        	irregular: true,
                            layerOptions: {styleMap: this.styleMap}
                        }
                    }
                ),
                controlCircle : new OpenLayers.Control.Measure(
                    OpenLayers.Handler.RegularPolygon, {
                        persist: true,
                        type :'circle',
                        handlerOptions: {
                        	irregular: false,
                        	sides: 40,
                            layerOptions: {styleMap: this.styleMap}
                        }
                    }
                )
            };
            
            
	},
	//返回组装好的地图切换控件dom对象
	getElement : function(){
	    var $this=this;
		if(!this.element){
			if(this.config){
				this.element = this.createElement(this.map);	
				for(var key in this.measureControls) {
					//添加地图控件
	                var control = this.measureControls[key];
	                /*
	                control.events.on({
	                    "measure": function(event){
	                    	var geometry = event.geometry;
				            var units = event.units;
				            var order = event.order;
				            var measure = event.measure;
				            var type=event.object.type;
				            if(type=='line'){
				            	Utils.measureAlert(measure.toFixed(3)+units,"测量距离");
				            }else if(type=='polygon'){
				            	Utils.measureAlert(measure.toFixed(3)+units+"<sup>2</" + "sup>","测量面积");
				            }else if(type=='rect'){
				            	//Utils.measureAlert("rect","矩型拉框");
				            	var components=geometry.components[0];
				            	var box;
				            	//	.transform($this.map.getProjectionObject(),$this.map.displayProjection);
				            }
	                    },
	                    "measurepartial": function(event){
	                    }
	                });
	                */
	                control.events.register('measure',$this.map,function(event){
	                		var geometry = event.geometry;
				            var units = event.units;
				            var order = event.order;
				            var measure = event.measure;
				            var type=event.object.type;
				            if(type=='line'){
				            	Utils.measureAlert(measure.toFixed(3)+units,"测量距离");
				            }else if(type=='polygon'){
				            	Utils.measureAlert(measure.toFixed(3)+units+"<sup>2</" + "sup>","测量面积");
				            }else if(type=='boxZooIn'){
				            	var components=geometry.components[0];
				            	var lonlats=components;
				            	var box=lonlats.components;
				            	//取得拉框的矩形的Bounds
				            	var bounds=new OpenLayers.Bounds(box[0].x,box[0].y,box[2].x,box[2].y);
				            	var center = bounds.getCenterLonLat();
				            	var lastZoom = this.getZoom(); 
				            	this.zoomToExtent(bounds);
				            	if(lastZoom == this.getZoom()){
				            		this.zoomTo(this.getZoom() + 1);
				            	}
				            	
				            }else if(type=='boxZooOut'){
				            	var components=geometry.components[0];
				            	var lonlats=components;
				            	var box=lonlats.components;
				            	//取得拉框的矩形的Bounds
				            	var bounds=new OpenLayers.Bounds(box[0].x,box[0].y,box[2].x,box[2].y);
				            	var center = bounds.getCenterLonLat();
				            	var lastZoom = this.getZoom(); 
				            	//this.zoomToExtent(bounds);
				            	this.zoomTo(this.getZoom() - 1);
				            	if(lastZoom == this.getZoom()){
				            		this.zoomTo(this.getZoom() - 1);
				            	}
				            	
				            }
	                });
	                this.map.addControl(control);
            	}
			}
		}
		return this.element;
	},
	createElement : function(){
		var $this=this;
		var MapToolBox=$("<div></div>");
		if( this.config.top!=null)MapToolBox.css('top' ,this.config.top+'px');
		if( this.config.right!=null)MapToolBox.css('right' ,this.config.right+'px');
		if( this.config.left!=null)MapToolBox.css('left' ,this.config.left+'px');
		if( this.config.bottom!=null)MapToolBox.css('bottom' ,this.config.bottom+'px');
		MapToolBox.addClass('widget-class');
		MapToolBox.css({
			'width' :'423px',
			'height' : '35px',
			'font-size' :'12px'
		});
		var MapToolBar=$('<div id="MapToolBar">'+
				'<button class="toggleControl" name="none">平移</button>'+
		        '<button class="toggleControl" name="line">测量距离</button>'+
				'<button class="toggleControl" name="polygon">测量面积</button>'+
				'<button class="toggleControl" name="boxZooIn">拉框放大</button>'+
				'<button class="toggleControl" name="boxZooOut">拉框缩小</button>'+
				//'<button class="toggleControl" name="controlCircle">画圆</button>'+
			'</div>');
		MapToolBar.css({
			'font-size' :'12px'
		}).find('.toggleControl').click(function(){
			var name=$(this).attr('name');
			$this.toggleControl(name);
		});
		MapToolBox.append(MapToolBar);
		MapToolBar.buttonset();
		return MapToolBox;
	},
	toggleControl : function(type){
		for(key in this.measureControls) {
              var control = this.measureControls[key];
              if(type == key ) {
                  control.activate();
              } else {
                  control.deactivate();
              }
        }
	},
	CLASS_NAME:"openGis.MapToolWidget"
 });